/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package indusbank;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author TQNINH
 */
public class Account {

    private String firstName;
    private String lastName;
    private int age;
    private float accountBalance;
    
    public Account() {
        
    }
    
    public void addAccount() {
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter the account holder's First Name: ");
        this.firstName = scan.next();
        
        System.out.print("Enter the account holder's Last Name: ");
        this.lastName = scan.next();
        
        System.out.print("Enter the account holder's age: ");
        this.age = scan.nextInt();
        
        System.out.print("Enter the starting balance of the account: ");
        this.accountBalance = scan.nextFloat();
        
        this.saveState();
    }
    
    public void saveState() {
        try {
            FileWriter fwriter = new FileWriter("Account.txt", true);
            BufferedWriter writer = new BufferedWriter(fwriter);

            writer.write(this.lastName);
            writer.write("\t");
            writer.write(this.firstName);
            writer.write("\t");
            writer.write(Integer.toString(this.age));
            writer.write("\t");
            writer.write(Float.toString(this.accountBalance));
            writer.write("\t");
            writer.newLine();
            writer.close();
            fwriter.close();
        } catch (FileNotFoundException e) {
            System.out.println("The requested file cannot be found.");
        } catch (IOException e) {
            System.out.println("An error occured while writing to the file.");
        }
    }
    
    public void display() {
        System.out.println(" **** Account Details ****");
        System.out.format("%1$-1s | %2$s | %3$5s | %4$-10s | \n",
                "Last Name", "First Name", "Age", "Account Balance($)");
        try {
            FileReader input = new FileReader("Account.txt");
            BufferedReader reader = new BufferedReader(input);
            String record;
            while((record = reader.readLine()) != null) {
                StringTokenizer token = new StringTokenizer(record);
                while(token.hasMoreTokens()) {
                    System.out.print(token.nextToken("\t") + "\t\t");
                }
                System.out.println("");
            }
            reader.close();
            input.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
